if (getCookie('user') != undefined) {
    $('.fli>a').click(function () {
        $(this).parent().find('.sul').stop().slideToggle(100)
        // $(this).addClass('fa-checked').parent().siblings().children('a').removeClass('fa-checked')
    })

    $('.sli>a').click(function () {
        $(this).parents('.fli').children('a').addClass('fa-checked').parent().siblings().children('a').removeClass('fa-checked')
        $(this).addClass('sa-checked').parent().siblings().children('a')
            .removeClass('sa-checked').parents('.fli').siblings().find('.sli')
            .children('a').removeClass('sa-checked')
        $('.r-top').html(`
           ${$(this).parents('.sul').prev().text()} > ${$(this).text()}
        `)
    })

    $('.ful').on('click', '.sy,.exit', function () {
        $(this).parents('.ful').find('.sli').children('a').removeClass('sa-checked')
        $(this).addClass('fa-checked').parent().siblings().children('a').removeClass('fa-checked')
        $('.r-top').html(`
            ${$(this).text()}
        `)
    })

    // 管理员显示的页面信息
    // console.log(getCookie('type'));
    $('.t-name>span').text(getCookie('name'))
    $('.info>div').text(getCookie('name').charAt(0))
    $('.t-name>p>em').text(getCookie('name'))
    xrzy(getCookie('type'))


    // 菜单栏点击事件
    $('.ful').on('click', '.sy,.zjwz,.xzwz,.sywz>a,.grzx,.syzh>a,.exit', function (e) {
        if ($(e.target).is('.sy')) {
            xrzy(getCookie('type'))
        } else if ($(e.target).is('.syzh>a')) {
            $('.content').empty().append(`
            <div class="r-con-syzh">
                        <div class="syzh-top">
                            <button type="button" class="btn btn-primary delAll">🥅<em>批量删除</em></button>
                            <button type="button" class="btn btn-primary newBul">🏹<em>新建账号</em></button>
                            <input type="text" class="form-control" placeholder="账号">
                            <button type="button" class="btn btn-primary ss">🔍<em>搜索</em></button>
                        </div>
                        <div class="syzh-th">
                            <table class="table">
                                <tr class="th-head">
                                    <th>
                                        <span class="sn-fa">
                                            <span class="sn-son" type="0"></span>
                                        </span>
                                    </th>
                                    <th>用户名</th>
                                    <th>账号</th>
                                    <th>密码</th>
                                    <th>操作</th>
                                </tr>
                            </table>
                        </div>
                        <div class="syzh-con">
                            <table class="table">
                            </table>
                        </div>
                        <div class="syzh-bto">
                        </div>
                    </div>
            `)
            xryh()


        } else if ($(e.target).is('.grzx')) {
            grzx()
        } else if ($(e.target).is('.xzwz')) {
            $('.content').empty().append(`
            <div class="addAtr">
                <div>
                    <form class="form-horizontal">
                        <div class="form-group">
                        <label for="wz-title" class="col-sm-2 control-label">标题</label>
                        <div class="col-sm-10">
                            <input type="text" autocomplete="off" class="form-control" id="wz-title" placeholder="标题">
                        </div>
                        </div>
                        <div class="form-group">
                        <label for="wz-con" class="col-sm-2 control-label">内容</label>
                        <div class="col-sm-10">
                            <textarea class="form-control" id="wz-con" rows="18"></textarea>
                        </div>
                        </div>
                        <div class="form-group">
                            <button type="button" class="btn btn-primary fb">发布</button>
                        </div>
                    </form>
                </div>
            </div>
            `)
        } else if ($(e.target).is('.sywz>a')) {
            $('.content').empty().append(`
            <div class="r-con-syzh">
                <div class="syzh-top">
                    <button type="button" class="btn btn-primary delAll">🥅<em>批量删除</em></button>
                    <input type="text" class="form-control" placeholder="标题">
                    <button type="button" class="btn btn-primary ss">🔍<em>搜索</em></button>
                </div>
                <div class="syzh-th">
                    <table class="table">
                        <tr class="th-head">
                            <th>
                                <span class="sn-fa">
                                    <span class="sn-son" type="0"></span>
                                </span>
                            </th>
                            <th class="wz-id">id</th>
                            <th class="wz-name">用户名</th>
                            <th class="wz-tit">标题</th>
                            <th>内容</th>
                            <th class="wz-time">发表时间</th>
                            <th class="wz-cz">操作</th>
                        </tr>
                    </table>
                </div>
                <div class="syzh-con">
                    <table class="table">
                    </table>
                </div>
                <div class="syzh-bto">
                </div>
            </div>
            `)
            xrwz()
        } else if ($(e.target).is('.zjwz')) {
            $('.content').empty().append(`
            <div class="r-con-syzh">
                <div class="syzh-top">
                    <button type="button" class="btn btn-primary delAll">🥅<em>批量删除</em></button>
                    <input type="text" class="form-control" placeholder="标题">
                    <button type="button" class="btn btn-primary ss">🔍<em>搜索</em></button>
                </div>
                <div class="syzh-th">
                    <table class="table">
                        <tr class="th-head">
                            <th>
                                <span class="sn-fa">
                                    <span class="sn-son" type="0"></span>
                                </span>
                            </th>
                            <th class="wz-id">id</th>
                            <th class="wz-tit">标题</th>
                            <th>内容</th>
                            <th class="wz-time">发表时间</th>
                            <th class="wz-cz">操作</th>
                        </tr>
                    </table>
                </div>
                <div class="syzh-con">
                    <table class="table">
                    </table>
                </div>
                <div class="syzh-bto">
                </div>
            </div>
            `)
            xrzjwz()
        } else if ($(e.target).is('.exit')) {
            // 退出按钮
            if (confirm('确定退出?')) {
                removeCookie('user')
                removeCookie('name')
                removeCookie('type')
                window.location.href = '../html/login.html'
            }
        }

    })

} else {
    let t = 5
    $('body').html(`你未登录，5秒后返回登录页面`).css({
        'text-align': 'center',
        'font-size': '80px',
        'color': 'red'
    })
    setInterval(() => {
        t--;
        $('body').html(`你未登录，${t}秒后返回登录页面`)
        if (t == 0) {
            window,
            location.href = '../html/login.html'
        }
    }, 1000)
}

// 选择框全选
// console.log(
//     $('.check-all'));
$('.content').on('click', '.th-head .sn-son', function () {
    if ($(this).attr('type') === '0') {
        $(this).attr('type', '1')
        $(this).parent().addClass('is-check')
        $(this).parent().removeClass('is-indet')
        $('.tr-con').find('.sn-fa').addClass('is-check')
            .children('.sn-son').attr('type', '1')

    } else {
        $(this).attr('type', '0')
        $(this).parent().removeClass('is-check')
        $('.tr-con').find('.sn-fa').removeClass('is-check')
            .children('.sn-son').attr('type', '0')
    }
})

// 单个选择
$('.content').on('click', '.tr-con .sn-son', function () {
    var flag = true;
    var flag2 = false;
    if ($(this).attr('type') === '0') {
        $(this).attr('type', '1')
        $(this).parent().addClass('is-check')
    } else {
        $(this).attr('type', '0')
        $(this).parent().removeClass('is-check')

    }
    $('.tr-con .sn-son').each(function () {
        if ($(this).attr('type') === '0') {
            flag = false
        }
        if ($(this).attr('type') === '1') {
            flag2 = true
        }
    })

    if (!flag) {
        $('.th-head .sn-fa').removeClass('is-check')
        $('.th-head .sn-fa').addClass('is-indet')
    } else {
        $('.th-head .sn-fa').removeClass('is-indet')
        $('.th-head .sn-fa').addClass('is-check')
            .children('.sn-son').attr('type', '1')
    }
    if (!flag2) {
        $('.th-head .sn-fa').removeClass('is-check')
        $('.th-head .sn-fa').removeClass('is-indet')
    }
})

// 添加用户弹出模拟框
$('.content').on('click', '.newBul', function () {
    $('#newUser').modal('show')
    $('#newUser').find('h4').text('添加用户')
    $('#newUser').attr('or', '1')
})
// 修改用户弹出模拟框
$('.content').on('click', '.xg', function () {
    $('#newUser').modal('show')
    $('#newUser').find('h4').text('修改用户')
    $('#newUser').attr('or', '2')
    $('#name').val($(this).parents('tr').children('td').eq(1).text())
    $('#user').val($(this).parents('tr').children('td').eq(2).text())
    $('#pwd').val($(this).parents('tr').children('td').eq(3).text())
    $('#user').attr('disabled', 'disabled')

})
// 个人中心修改弹出模拟框
$('.content').on('click', '.zx', function () {
    $('#newUser').modal('show')
    $('#newUser').find('h4').text('好好学习，天天向上')
    $('#newUser').attr('or', '3')
    storage.get('RT').forEach(function (item) {
        if (getCookie('user') == item['user']) {
            $('#name').val(item['name'])
            $('#user').val(item['user'])
            $('#pwd').val(item['pwd'])
        }
    })
    $('#user').attr('disabled', 'disabled')
})

// 模拟框隐藏时执行
$('#newUser').on('hidden.bs.modal', function () {
    $('#user').removeAttr('disabled')
    $('#name').val('')
    $('#user').val('')
    $('#pwd').val('')
})

// 添加用户和修改用户
$('.tjyh').click(function () {
    if ($('#newUser').attr('or') == '1') {
        let name = $('#name').val()
        let user = $('#user').val()
        let pwd = $('#pwd').val()
        if (name == '') {
            ts('#name', 'tooltip', '用户名不能为空', 'top')
        } else if (user == '') {
            ts('#user', 'tooltip', '账号不能为空', 'top')
        } else if (pwd == '') {
            ts('#pwd', 'tooltip', '密码不能为空', 'top')
        } else {
            if (operUser.see(user)) {
                $('#newUser').modal('hide')
                operUser.add(user, pwd, name)
                // 添加完用户渲染一遍
                xryh()
                $('#name').val('')
                $('#user').val('')
                $('#pwd').val('')

            } else {
                ts('#user', 'tooltip', '账号存在', 'top')
            }

        }
    } else if ($('#newUser').attr('or') == '2') {
        let name = $('#name').val()
        let user = $('#user').val()
        let pwd = $('#pwd').val()
        if (name == '') {
            ts('#name', 'tooltip', '用户名不能为空', 'top')
        } else if (pwd == '') {
            ts('#pwd', 'tooltip', '密码不能为空', 'top')
        } else {
            operUser.modify(user, pwd, name);
            $('#newUser').modal('hide')
            xryh();
        }
    } else if ($('#newUser').attr('or') == '3') {
        let name = $('#name').val()
        let user = $('#user').val()
        let pwd = $('#pwd').val()
        if (name == '') {
            ts('#name', 'tooltip', '用户名不能为空', 'top')
        } else if (pwd == '') {
            ts('#pwd', 'tooltip', '密码不能为空', 'top')
        } else {
            operUser.modify(user, pwd, name);
            $('#newUser').modal('hide')
            grzx();
        }
    }

})

// 删除用户
$('.content').on('click', '.sc', function () {
    operUser.del($(this).parents('tr').children('td').eq(2).text())
    xryh();
})

// 获取用户渲染到页面上
function xryh(index = 1) {
    $('.syzh-con .table').empty()
    storage.get("RT").forEach((item, ind) => {
        if (ind >= ((index - 1) * 7) && ind < (index * 7)) {
            $('.syzh-con .table').append(`
                <tr class="tr-con">
                    <td>
                        <span class="sn-fa">
                            <span class="sn-son" type="0"></span>
                        </span>
                    </td>
                    <td>${item['name']}</td>
                    <td>${item['user']}</td>
                    <td>${item['pwd']}</td>
                    <td>
                        <a class="xg" href="#">🖊<em>修改</em></a>
                        <a class="sc" href="#">🗑<em>删除</em></a>
                    </td>
                </tr>
            `)
        }

    })

    $('.r-con-syzh .syzh-bto').empty().append(`
        
        <span>共<em>${storage.get("RT").length}</em>条</span>
        <button class="syzh-bto-l"><</button>
        <span class="ppp">${index}</span>
        <button class="syzh-bto-r">></button>
        `)
}

// 用户数据 下一页和上一页
$('.content').on('click', '.syzh-bto-l,.syzh-bto-r', function (e) {
    let index = $('.ppp').text() - 0
    if ($(e.target).is('.syzh-bto-l')) {
        if (index !== 1) {
            xryh(--index)
        }
    } else {
        let ind = Math.ceil((storage.get('RT').length - 0) / 7)
        console.log(index, ind);
        if (index < ind) {
            xryh(++index)
        }
    }
})

// 文章数据 下一页和上一页
$('.content').on('click', '.sywz-bto-l,.sywz-bto-r', function (e) {
    let index = $('.ppp').text() - 0
    if ($(e.target).is('.sywz-bto-l')) {
        if (index !== 1) {
            xrwz(--index)
        }
    } else {
        let ind = Math.ceil((storage.get('RW').length - 0) / 7)
        console.log(index, ind);
        if (index < ind) {
            xrwz(++index)
        }
    }
})

// 自己文章数据 下一页和上一页
$('.content').on('click', '.zjwz-bto-l,.zjwz-bto-r', function (e) {
    let index = $('.ppp').text() - 0
    if ($(e.target).is('.zjwz-bto-l')) {
        if (index !== 1) {
            xrzjwz(--index)
        }
    } else {
        let ind = Math.ceil((operAtr.see(getCookie('user')).length - 0) / 7)
        console.log(index, ind);
        if (index < ind) {
            xrzjwz(++index)
        }
    }
})

// 判断早,中,晚
function eveday() {
    now = new Date(), hour = now.getHours()
    if (hour < 6) {
        return "凌晨好！"
    } else if (hour < 9) {
        return "早上好！"
    } else if (hour < 12) {
        return "上午好！"
    } else if (hour < 14) {
        return "中午好！"
    } else if (hour < 17) {
        return "下午好！"
    } else if (hour < 19) {
        return "傍晚好！"
    } else if (hour < 22) {
        return "晚上好！"
    } else {
        return "夜里好！"
    }
}

// 渲染主页
function xrzy(type) {
    var nstr = ''
    var sper = ''
    var index = 0
    if (type == '0') {
        nstr = '超级管理员';
        sper = `
        <div class="per">
            <div>人</div>
            <p><em>${storage.get("RT").length}</em><span>好好学习</span></p>
        </div>
        `
        index = storage.get("RW").length
    } else {
        nstr = '普通用户'
        sper = `
        <div class="per sper">好好学习，天天向上</div>
        `;
        storage.get("RW").forEach(item => {
            if (item['ruser'] == getCookie('user')) {
                index++;
            }
        })
        $('.sywz').hide()
        $('.syzh').hide()
    }
    $('.content').empty().append(`
    <div class="r-con">
        <div class="r-con-t">
            <div class="info">
                <div>${getCookie('name').charAt(0)}</div>
                <p><em>${getCookie('name')}</em><span>${nstr}</span></p>
            </div>
            ${sper}
            <div class="wzs">
                <div>文</div>
                <p><em>${index}</em><span>天天向上</span></p>
            </div>
        </div>
    </div>
    `)
}

// 渲染个人中心页面
function grzx() {
    storage.get('RT').forEach(function (item) {
        if (getCookie('user') == item['user']) {
            $('.content').empty().append(`
            <div class="per-cen">
                <div>
                    <h3>${eveday()}</h3>
                    <p>只要持之以恒,知识丰富了,终能发现其奥秘。</p>
                    <p><em>用户名:</em><span class="per-con-name">${item['name']}</span></p>
                    <p><em>账号:</em><span class="per-con-user">${item['user']}</span></p>
                    <p><em>密码:</em><span class="per-con-pwd">${item['pwd']}</span></p>
                    <button class="btn btn-primary zx" type="button">修改信息</button>
                </div>
            </div>
            `)
        }
    })

}

// 发布文章
$('.content').on('click', '.fb', function () {
    let tit = $('#wz-title').val();
    let con = $('#wz-con').val();
    if (tit == '') {
        ts('#wz-title', 'tooltip', '标题不能为空', 'top')
    } else if (con == '') {
        ts('#wz-con', 'tooltip', '内容不能为空', 'top')
    } else {
        operAtr.add(getCookie('user'), getCookie('name'), tit, con)
        alert('发布成功')
        $('#wz-title').val('');
        $('#wz-con').val('');
    }

})

// 渲染所有文章
function xrwz(index = 1) {
    $('.syzh-con .table').empty()
    storage.get("RW").forEach((item, ind) => {
        if (ind >= ((index - 1) * 7) && ind < (index * 7)) {
            $('.syzh-con .table').append(`
                <tr class="tr-con">
                    <td>
                        <span class="sn-fa">
                            <span class="sn-son" type="0"></span>
                        </span>
                    </td>
                    <td class="wz-id">${item['rid']}</td>
                    <td class="wz-name">${item['rname']}</td>
                    <td class="wz-tit">${item['rtitle']}</td>
                    <td class="wz-con">${item['rcon']}</td>
                    <td class="wz-time">${getDate('yyyy-mm-dd',item['rtime'])}</td>
                    <td class="wz-hide">${item['ruser']}</td>
                    <td class="wz-cz">
                        <a class="wz-xg" href="#">🖊<em>修改</em></a>
                        <a class="wz-sc" href="#">🗑<em>删除</em></a>
                    </td>
                </tr>
            `)
        }

    })

    $('.r-con-syzh .syzh-bto').empty().append(`
        
        <span>共<em>${storage.get("RW").length}</em>条</span>
        <button class="sywz-bto-l"><</button>
        <span class="ppp">${index}</span>
        <button class="sywz-bto-r">></button>
        `)
}

// 渲染自己文章
function xrzjwz(index = 1) {
    $('.syzh-con .table').empty()
    operAtr.see(getCookie('user')).forEach((item, ind) => {
        if (ind >= ((index - 1) * 7) && ind < (index * 7)) {
            $('.syzh-con .table').append(`
                <tr class="tr-con">
                    <td>
                        <span class="sn-fa">
                            <span class="sn-son" type="0"></span>
                        </span>
                    </td>
                    <td class="wz-id">${item['rid']}</td>
                    <td class="wz-tit">${item['rtitle']}</td>
                    <td class="wz-con">${item['rcon']}</td>
                    <td class="wz-time">${getDate('yyyy-mm-dd',item['rtime'])}</td>
                    <td class="wz-hide">${item['ruser']}</td>
                    <td class="wz-cz">
                        <a class="wz-xg" href="#">🖊<em>修改</em></a>
                        <a class="wz-sc" href="#">🗑<em>删除</em></a>
                    </td>
                </tr>
            `)
        }

    })

    $('.r-con-syzh .syzh-bto').empty().append(`
        
        <span>共<em>${operAtr.see(getCookie('user')).length}</em>条</span>
        <button class="zjwz-bto-l"><</button>
        <span class="ppp">${index}</span>
        <button class="zjwz-bto-r">></button>
        `)
}

// 修改文章
$('.content').on('click', '.wz-xg', function () {
    $('#mnk-wzxg').modal('show')
    $('#mnk-xg-tit').val($(this).parents('tr').children('.wz-tit').text())
    $('#mnk-xg-con').val($(this).parents('tr').children('.wz-con').text())
    $('.wz-id').val($(this).parents('tr').children('.wz-id').text())
    $('.wz-user').val($(this).parents('tr').children('.wz-hide').text())
})

// 修改文章确定按钮
$('.mnk-xg-qd').click(function () {
    let tit = $('#mnk-xg-tit').val();
    let con = $('#mnk-xg-con').val();
    let wzid = $('.wz-id').val();
    let wzuser = $('.wz-user').val()
    if (tit == '') {
        ts('#mnk-xg-tit', 'tooltip', '标题不能为空', 'top')
    } else if (con == '') {
        ts('#mnk-xg-con', 'tooltip', '内容不能为空', 'top')
    } else {
        operAtr.modify(wzuser, wzid, tit, con)
        xrwz()
        $('#mnk-wzxg').modal('hide')
        $('#mnk-xg-tit').val('');
        $('#mnk-xg-con').val('');
    }
})

// 删除文章按钮
$('.content').on('click', '.wz-sc', function () {
    console.log(1);
    let rid = $(this).parents('tr').children('td').eq(1).text();
    let user = $(this).parents('tr').children('td').eq(6).text();

    operAtr.del(user, rid)
    xrwz()
})

